package com.doordu.soa.service.comm.utils;

import java.util.Random;

/**
 * 随机字符串工具类
 * Created by marvin on 2017/6/4.
 */
public class RandomUtil {

    /**
     * 获取指定长度的随机字符串
     * @param mode 字符模式类型
     * @param strOutLength 输出的字符串长度
     * @return
     */
    public static String getRandomStr(int mode, int strOutLength){
        String str = "";
        switch (mode) {
            case 1:
                str = "1234567890";
                break;
            case 2:
                str = "abcdefghijklmnopqrstuvwxyz";
                break;
            case 3:
                str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                break;
            case 4:
                str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                break;
            case 5:
                str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                break;
            case 6:
                str = "abcdefghijklmnopqrstuvwxyz1234567890";
                break;
            case 7:
                str = "123456789bcde";
                break;
            default:
                str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
                break;
        }

        int strLength = str.length();
        Random random = new Random();
        int min = 0;
        int max = strLength;
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<strOutLength;i++){
            int index = random.nextInt(max - min) + min;
            String tempChar = str.substring(index, index+1);
            if(sb.toString().length()==strOutLength){
                break;
            }
            sb.append(tempChar);
        }

        return sb.toString();
    }

    public static void main(String args[]){
        String randomCode = RandomUtil.getRandomStr(7,18);
        String initCode = DateUtils.getCurrentDateTimeStr(DateUtils.DATE_TIMESTAMP_SEC_FMT);
        System.out.println(randomCode);
        System.out.println(initCode);
    }
}
